/**
 * @date 1:03:38 PM
 * EducationSMSDaoImpl.java
 * Administrator
 * TODO
 */
package com.qs.education.bean.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.stereotype.Repository;

import com.qs.education.bean.api.ResponseListBean;
import com.qs.education.bean.dao.IEducationSMSDao;
import com.qs.education.bean.entity.EducationSMS;

/**
 * @class EducationSMSDaoImpl	
 * @author Warren
 * @description
 * @date Nov 2, 2018 
 */
@Repository
public class EducationSMSDaoImpl extends BaseDaoImpl<EducationSMS> implements IEducationSMSDao{
	
	/**
	 * @author Warren
	 * @param mobile
	 * @return EducationSMS
	 * @description
	 * @date Nov 2, 2018
	 */
	
	@Override
	@Transactional 
	public EducationSMS findEducationSMSByMobile(String mobile) {
		String jql="Select es from EducationSMS es where es.mobile=:mobile order by es.sendTime desc";
		Query query = this.em.createQuery(jql);
		query.setParameter("mobile", mobile);
		@SuppressWarnings("unchecked")
		List<EducationSMS> list = query.getResultList();
		if(list != null && list.size()>0){
			return list.get(0);
		} else {
			return null;
		}
	}

	/**
	 * @author Warren
	 * @param es
	 * @return EducationSMS
	 * @description
	 * @date Nov 2, 2018
	 */
	@Override
	@Transactional
	public boolean saveEducationSMS(EducationSMS es) {
		return save(es);
	}

	/**
	 * @author Warren
	 * @param es
	 * @return boolean
	 * @description
	 * @date Nov 5, 2018
	 */
	@Override
	@Transactional
	public boolean updateEducationSMS(EducationSMS es) {
		return update(es);
	}

	@Override
	public int getSMSCount(String mobile) {

		String jql = "Select COUNT(*) from EducationSMS es where es.mobile like :mobile";
		Map<String,Object> params = new HashMap<>();
		params.put("mobile", "%"+mobile+"%");
		List<?> result = query(jql,params);
		if(null!=result && !result.isEmpty()) {
			return Integer.parseInt(result.get(0).toString());
		}
		return 0;
		
	}
	
	@Override
	public List<EducationSMS> getSMSList(String mobile, Integer currentPage, Integer pageSize) {
		
		String jql = "Select es from EducationSMS es where es.mobile like :mobile";
		Map<String,Object> params = new HashMap<>();
		params.put("mobile", "%"+mobile+"%");
		int firstResult = (currentPage - 1) * pageSize;
		List<EducationSMS> list  = query(jql,EducationSMS.class,firstResult,pageSize,params);
		
		return list;
	}

	

}
